package com.thoughtworks.studio.tools.graphics;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import com.thoughtworks.studio.tools.cardkit.CardMetrics;

public class ImageGenerator {
	private String outputfile = null;
	private FileOutputStream fileoutputstream = null;

	int width = 95, height = 65;
	 private Logger logger= Logger.getLogger(ImageGenerator.class);
	public void outputImage(BufferedImage image, String path, String imgtype)
			throws FileNotFoundException, IOException {
		File file = new File(path);
		if (file.exists())
			file.delete();
		fileoutputstream = new FileOutputStream(path);
		ImageIO.write(image, imgtype, fileoutputstream);

		// close stream
		fileoutputstream.flush();
		fileoutputstream.close();
	}

	/**
	 * @param g
	 */
	public void drawFrame(Graphics g) {
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, width - 1, height - 1);
	}

	/**
	 * @param g
	 */
	public void fillBackground(Graphics g) {
		g.setColor(Color.white);
		g.fillRect(0, 0, width, height);
	}

	/**
	 * Create a Image
	 * 
	 * @return
	 */
	public BufferedImage createImage(CardMetrics property) {
		BufferedImage image = new BufferedImage(property.getWidth(), property
				.getHight(), property.getColorType());
		return image;
	}

	public void drawText(Graphics g, String text, Font font, Color color,
			int x, int y, boolean wrapped) {
		Color oldcolor = g.getColor();
		Font oldfont = g.getFont();
		int lineheight = g.getFontMetrics().getHeight();
		g.setColor(color);
		g.setFont(font);
		if (wrapped) {
			List<String> sagments = this.splitString(g, font, text);
			int count = 0;
			for (Iterator<String> iterator = sagments.iterator(); iterator
					.hasNext();) {
				String textline = iterator.next();
				g.drawString(textline, x , y + lineheight * count++);
			}
		} else {
			g.drawString(text, x, y);
		}
		g.setColor(oldcolor);
		g.setFont(oldfont);
	}

	public void drawText(Graphics g, String text, int x, int y, boolean wrapped) {
		drawText(g, text, g.getFont(), g.getColor(), x, y, wrapped);
	}

	public ArrayList<String> splitString(Graphics g, Font font, String str) {
		Font oldfont = g.getFont();
		g.setFont(font);
		FontMetrics metrics = g.getFontMetrics();
		ArrayList<String> result = new ArrayList<String>();
		String oldone= null;
		StringBuffer newone = new StringBuffer();
		int startIndex = 0;
		String[] words = str.split(" ");
		for (int i = 0; i < words.length; i++) {
			oldone = newone.toString();
			newone =newone.append(words[i]).append(" ");
			Rectangle2D rect = metrics.getStringBounds(newone.toString(), g);
			if (rect.getWidth() > CardMetrics.cardProperty.getWidth() - 2) {
				result.add(oldone);
				newone = new StringBuffer(words[i]);
			}
		}
		result.add(newone.toString());
		g.setFont(oldfont);
		return result;
	}

	public String getOutputfile() {
		return outputfile;
	}

	public void setOutputfile(String outputfile) {
		this.outputfile = outputfile;
	}

	public FileOutputStream getFileoutputstream() {
		return fileoutputstream;
	}

	public void setFileoutputstream(FileOutputStream fileoutputstream) {
		this.fileoutputstream = fileoutputstream;
	}

}
